//
//  EmojiManagerViewController.m
//  ThunderChat
//
//  Created by Kelly xu on 2019/3/12.
//  Copyright © 2019 Simubang. All rights reserved.
//

#import "EmojiManagerViewController.h"
#import "EmojiCollectionViewCell.h"
#import "EmojiListColCell.h"
#import <UICollectionViewLeftAlignedLayout/UICollectionViewLeftAlignedLayout.h>
#import "MyCourseListViewModel.h"
@interface EmojiManagerViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    
}
@property(weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(strong, nonatomic)NSMutableArray *emojiList;
/**
 靠左对齐的collectionView布局
 */
@property (nonatomic, strong) UICollectionViewLeftAlignedLayout *myFlowLayout;
@property(nonatomic,weak)IBOutlet UIImageView *listBg;
@property(nonatomic,strong)MyCourseListViewModel *myCourseListViewModel;
@end

@implementation EmojiManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"照片";
    [self.listBg setImage:[[UIImage imageNamed:@"cornerBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"cornerBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"cornerBg"].size.height/2]];
    [self configCollectionView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.myCourseListViewModel fetchCourseImgs:[NSString stringWithFormat:@"%ld",[NSDate date].year] month:[NSString stringWithFormat:@"%ld",[NSDate date].month] cmp:^(NSArray * _Nonnull imgList) {
        [self.emojiList removeAllObjects];
        [self.emojiList addObjectsFromArray:imgList];
        [self.collectionView reloadData];
    }];
}
-(void)configCollectionView{
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.delegate= self;
    _collectionView.dataSource = self;
//    _collectionView.collectionViewLayout = self.myFlowLayout;
    [_collectionView registerNib:[UINib nibWithNibName:@"EmojiCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"EmojiCollectionViewCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"EmojiListColCell" bundle:nil] forCellWithReuseIdentifier:@"EmojiListColCell"];
}
#pragma mark - UITableViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.emojiList count];
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EmojiListColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EmojiListColCell" forIndexPath:indexPath];
    [cell setCellValue:[self.emojiList objectAtIndex:indexPath.row]];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(85, 85+14);
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCourseImgModel *model = [self.emojiList objectAtIndex:indexPath.row];
    if (self.selectBlock) {
        self.selectBlock(model.usePicture, [NSString stringWithFormat:@"%.0f",model.addTime]);
    }
    [self.rt_navigationController popViewControllerAnimated:YES  complete:nil];
}
#pragma mark - 单例
-(NSMutableArray*)emojiList{
    if (!_emojiList) {
        _emojiList = [[NSMutableArray alloc]init];
    }
    return _emojiList;
}
- (UICollectionViewLeftAlignedLayout *)myFlowLayout
{
    if (!_myFlowLayout) {
        // 创建布局对象
        _myFlowLayout = [[UICollectionViewLeftAlignedLayout alloc] init];
        // 设置滑动方向
        _myFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        // 设置与屏幕边框间距
        _myFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _myFlowLayout;
}
#pragma mark - instance
-(MyCourseListViewModel*)myCourseListViewModel{
    if (!_myCourseListViewModel) {
        _myCourseListViewModel = [[MyCourseListViewModel alloc]init];
    }
    return _myCourseListViewModel;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
